<?php
class Contact_model extends Model {

    function contact_model()
    {
        parent::Model();
    }

	function countAllContacts()
	{
		$query = $this->db->get("contact_info");
		return $query->num_rows();
	}

	function countContactRFQs($contact_id)
	{
		$this->db->where('contact_id', $contact_id);
		return $this->db->get('rfq')->num_rows();
	}

	function getAllContacts()
	{
		$this->db->orderby('last_name', 'asc');
		return $this->db->get('contact_info');
	}
	function getContacts($offset=0, $limit=100)
	{
		$this->db->orderby('contact_id', 'asc');
		$this->db->groupby('last_name', 'asc');
		$this->db->offset($offset);
		$this->db->limit($limit);
		return $this->db->get('contact_info');
	}	
	
	function getAllContactInfo($id)
    {
		$this->db->where('contact_id', $id);
		return $this->db->get('contact_info')->row();
    }

	function getSingleContact($id)
	{
		$this->db->where('contact_id', $id);
		return $this->db->get('contact_info');
	}

	function addContact($contactInfo)
	{
		$this->db->insert('contact_info', $contactInfo);
		return TRUE;
	}

	function updateContact($contact_id, $contactInfo)
	{
		$this->db->where('contact_id', $contactInfo['contact_id']);
		$this->db->update('contact_info', $contactInfo);
		return TRUE;
	}
	
	function deleteContact($contact_id)
	{
		// Don't allow the admin to be deleted this way
		if ($contact_id === 0) {
			return FALSE;
		} else {

			/**
			 * There are 5 tables of data to delete from in order to completely
			 * clear out record of this contact.
			 *
			 * Handle them 1 at a time for clarity and ease of maintenance.
			 */
		 
			$this->db->where('contact_id', $contact_id);
			$this->db->delete('contact_info'); 
 
			return TRUE;
		}
	}

}
?>
